package com.itheima.controller;

import com.itheima.dto.*;
import com.itheima.pojo.*;
import com.itheima.service.ChatService;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class ChatController {

    @Autowired
    private ChatService chatService;
    @Autowired
    private UserService userService;

    @PostMapping("/luanchChat")
    public Result luanchChat(@RequestBody LuanchMsg luanchMsg) throws IOException {

        Integer res = chatService.luanchChat(luanchMsg.getGroupName(), luanchMsg.getUserName());

        if (res == -1) return new Result(0, "该群聊已存在，请勿重复创建", null);
        else return new Result(1, "发起成功", null);
    }

    @PostMapping("/addFriend")
    public Result addFriend(@RequestBody AddMsg addMsg) {
        log.info("添加好友:{},{}", addMsg.getFriendName(), addMsg.getUserName());
        Integer res = chatService.addFriend(addMsg.getFriendName(), addMsg.getUserName());

        if (res == -1) return new Result(0, "该用户未注册,无法添加好友", null);
        else if (res == 0) return new Result(0, "已是您的好友，请勿重复添加", null);
        else return new Result(1, "添加成功", null);
    }

    @PostMapping("/addGroup")
    public Result addGroup(@RequestBody AddGroup addGroup) {
        log.info("添加群聊:{},{}", addGroup.getGroupName(), addGroup.getUserName());
        Integer res = chatService.addGroup(addGroup.getGroupName(), addGroup.getUserName());

        if (res == -1) return new Result(0, "未找到该群聊,无法添加", null);
        else if (res == 0) return new Result(0, "您已在该群，请勿重复添加", null);
        else return new Result(1, "添加成功", null);
    }

    /**
     * 获取聊天列表
     *
     * @param user
     * @return
     */
    @PostMapping("/getChatList")
    public Result getChatList(@RequestBody User user) {
        String userName = user.getUserName();
        log.info("获取群聊: {}", userName);
        List<Group> groups = chatService.getGroup(userName);
        List<Friend> friends = chatService.getFriend(userName);

        List<ChatMsg> chat = new ArrayList<>();
        for (Group group : groups) {

            String groupName = group.getName();
            String avatar = userService.getAvatarByName(groupName);
            Chat chatMsg = chatService.getContentByName(userName, groupName);
            String newMsg = chatMsg == null ? "暂无新消息" : chatMsg.getChatContent();
            chat.add(new ChatMsg(newMsg, avatar, groupName));
        }

        for (Friend friend : friends) {

            String friendName = friend.getName();
            String avatar = userService.getAvatarByName(friendName);
            Chat chatMsg = chatService.getContentByName(userName, friendName);
            String newMsg = chatMsg == null ? "暂无新消息" : chatMsg.getChatContent();
            chat.add(new ChatMsg(newMsg, avatar, friendName));
        }
        return Result.success(chat);
    }

    @GetMapping("/getMsg")
    public Result getMsg(@RequestParam String userName, @RequestParam String chatName) {
        Chat msg = chatService.getMsg(userName, chatName);
        log.info("{}获取消息from{}", userName, chatName);
        if (msg == null) return new Result(-1, null, null);
        List<Group> groups = chatService.getGroup(userName);
        for (Group group : groups) {
            if(group.getName().equals(chatName)){
                log.info("{}获取消息from{}", userName, chatName);
                String avatar = userService.getAvatarByName(msg.getUserName());
                log.info("消息发送者{}", msg.getUserName());
                if(msg.getUserName().equals(userName)) return new Result(-1, null, null);
                return new Result(msg.getChatId(), msg.getChatContent(), avatar);
            }
        }
        String avatar = userService.getAvatarByName(chatName);
        return new Result(msg.getChatId(), msg.getChatContent(), avatar);
    }

    @PostMapping("/sendMsg")
    public Result sendMsg(@RequestBody SendMsg msg) {
        log.info("{}发送消息给{}", msg.getUserName(), msg.getReceiverName());
        chatService.storeMsg(msg.getUserName(), msg.getReceiverName(), msg.getSendContent());
        return Result.success();
    }

    @PostMapping("/getGroup")
    public Result getGroup(@RequestBody User user) {
        log.info("获取群聊: {}", user.getUserName());
        chatService.getGroup(user.getUserName());
        return Result.success();
    }

    /**
     * 指定聊天会话，获取聊天记录
     *
     * @param friend
     * @return
     */
    @PostMapping("/getChatLog")
    public Result getChatLog(@RequestBody Friend friend) {
        String userName = friend.getUserName();//me
        String name = friend.getName();//friend
        if(name.equals("we")) return new Result(0, null, 0);
        log.info("历史记录: me:{},friend:{}", userName, name);

        List<Chat> chatLog = chatService.getChatLog(userName, name);
        return Result.success(chatLog);
    }


}
